package com.nlx.notes.module.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nlx.notes.module.entity.UserCurrentBook;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * <p>
 * 用户当前账本关联表 Mapper 接口
 * </p>
 *
 * @author nlx
 * @since 2025-11-29
 */
public interface UserCurrentBookMapper extends BaseMapper<UserCurrentBook> {

    /**
     * 根据用户ID获取当前账本关联记录
     * @param userId 用户ID
     * @return 用户当前账本关联记录
     */
    @Select("SELECT * FROM user_current_book WHERE user_id = #{userId} LIMIT 1")
    UserCurrentBook selectByUserId(@Param("userId") Long userId);

    /**
     * 更新用户的当前账本
     * @param userId 用户ID
     * @param bookId 账本ID
     * @return 更新记录数
     */
    @Update("INSERT INTO user_current_book (user_id, book_id, created_time, updated_time) " +
            "VALUES (#{userId}, #{bookId}, NOW(), NOW()) " +
            "ON DUPLICATE KEY UPDATE book_id = #{bookId}, updated_time = NOW()")
    int updateCurrentBook(@Param("userId") Long userId, @Param("bookId") Long bookId);
}